/*
 * Copyright (c) 2017-present, CV4J Contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.core.spatial.conv;

import com.cv4j.core.datamodel.ColorProcessor;
import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.core.filters.BaseFilter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 最小最大过滤
 *
 * @author dev
 * @date 2021/07/22
 */
public class MinMaxFilter extends BaseFilter {
    private boolean minFilter;
    private int radius;

    /**
     * 最小最大过滤
     */
    public MinMaxFilter() {
        minFilter = true;
        radius = 1;
    }

    /**
     * 设置半径
     *
     * @param radius 半径
     */
    public void setRadius(int radius) {
        this.radius = radius;
    }

    /**
     * 最小值滤波器
     *
     * @return boolean
     */
    public boolean isMinFilter() {
        return minFilter;
    }

    /**
     * 设置最小值滤波器
     *
     * @param minFilter 最小值滤波器
     */
    public void setMinFilter(boolean minFilter) {
        this.minFilter = minFilter;
    }

    /**
     * 得到半径
     *
     * @return int
     */
    public int getRadius() {
        return radius;
    }

    /**
     * 做的过滤器
     *
     * @param src src
     * @return {@link Optional<ImageProcessor>}
     */
    @Override
    public Optional<ImageProcessor> doFilter(ImageProcessor src) {
        int numOfPixels = width * height;
        byte[][] output = new byte[3][numOfPixels];

        int size = radius * 2 + 1;
        int total = size * size;
        int r1 = 0;
        int g1 = 0;
        int b1 = 0;
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                // 统计滤波器
                int[][] subpixels = new int[3][total];
                int index = 0;
                for (int i = -radius; i <= radius; i++) {
                    int roffset = row + i;
                    roffset = (roffset < 0) ? 0 : (roffset >= height ? height - 1 : roffset);
                    for (int j = -radius; j <= radius; j++) {
                        int coffset = col + j;
                        coffset = (coffset < 0) ? 0 : (coffset >= width ? width - 1 : coffset);
                        subpixels[0][index] = RED[roffset * width + coffset] & 0xff;
                        subpixels[1][index] = GREED[roffset * width + coffset] & 0xff;
                        subpixels[2][index] = BLUE[roffset * width + coffset] & 0xff;
                        index++;
                    }
                }

                Arrays.sort(subpixels[0]);
                Arrays.sort(subpixels[1]);
                Arrays.sort(subpixels[2]);

                if (minFilter) {
                    r1 = subpixels[0][0];
                    g1 = subpixels[1][0];
                    b1 = subpixels[2][0];
                } else {
                    r1 = subpixels[0][total - 1];
                    g1 = subpixels[1][total - 1];
                    b1 = subpixels[2][total - 1];
                }

                output[0][row * width + col] = (byte) r1;
                output[1][row * width + col] = (byte) g1;
                output[2][row * width + col] = (byte) b1;
            }
        }

        ((ColorProcessor) src).putRGB(output[0], output[1], output[2]);
        output = null;
        return Optional.ofNullable(src);
    }
}
